const ProductService = require("../../services/admin/ProductService")

const ProductController = {
  add:async (req,res) => {
    const cover = req.file ? `/productuploads/${req.file.filename}` : ''
    const { title,introduction,detail } = req.body
      await ProductService.add({title,introduction,detail,cover,editTime:new Date()})
      res.send({
        ActionType:'OK'
      })
  },
  getList:async (req,res) => {
    // 获取列表的接口要将数据返回出去
    const result = await ProductService.getList(req.params.id)
    res.send({
      ActionType:'OK',
      data:result
    })
  },
  delList:async (req,res) => {
    // 这里req.params.id是因为占位符后接的是:id
    await ProductService.delList({_id:req.params.id})
    res.send({
      ActionType:'OK'
    })
  },
  updateList:async (req,res) => {
    const cover = req.file ? `/productuploads/${req.file.filename}` : ''
    const { title,introduction,detail,_id } = req.body
      await ProductService.updateList({_id,title,introduction,detail,cover,editTime:new Date()})
      res.send({
        ActionType:'OK'
      })
  } 
}

module.exports = ProductController